package com.burgeon.backend.SHOPBACKEND.rest.invite;
/**
 * 邀约常用语
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.invite.Expressions;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QuerryExpressionsData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QuerryExpressionsData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public HashMap getData(String type) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(type), url);
        System.err.println(str);
        String code = new JSONArray(str.replace("null","-")).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        ArrayList<Expressions> expressionss = new ArrayList<Expressions>();
        JSONArray jsonArray = new JSONArray(str.replace("null","-").replace("N/A","-")).getJSONObject(0).getJSONArray("rows");

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            Expressions expressions = new Expressions();
            expressions.setId(array.getString(0));
            expressions.setType(array.getString(1));
            expressions.setValue(array.getString(2));
            expressionss.add(expressions);
        }
        HashMap data = new HashMap();
        data.put("list",expressionss);
        data.put("count",count);
        return data;
    }

    private MultiValueMap Data(String type) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("'RVISITLAE_TYPE'");//类型
        columns.add("RVISIT_CONTENT");//话术


        params.put("columns", columns);
        params.put("table", "APP_VIP_RVISITLAE");//表名

        params.put("count", "true");



        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号
        String ac = "'"+type+"'";
        paramss.put("column", "'RVISITLAE_TYPE'");
        paramss.put("condition", ac);
        //params.put("range",1000);
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        QuerryExpressionsData querryYCDisStatus = new QuerryExpressionsData("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(querryYCDisStatus.getData("2"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
